<template>
  <div class="common-layout">
    <el-container >
      <el-aside width="260px">
<!--        机构树-->
          <el-input prefix-icon="el-icon-search" v-model="input" placeholder="请输入部门名称" :clearable="true" />
          <el-tree
            class="tree"
            ref="treeRef"
            :data="treeList"
            :props="treeProps"
            :filter-node-method="onTreeFilter"
            :expand-on-click-node="false"
            highlight-current
            default-expand-all
            node-key="id"
            @node-click="onTreeClick"
          />
      </el-aside>
      <el-container>
        <el-header class="no-flex" height="auto">
          <el-form
            class="sys-filters"
            ref="formRef"
            inline
            :model="form"
            label-width="70px"
          >
            <el-form-item label="用户名称" prop="username">
              <el-input
                style="width: 200px"
                placeholder="请输入登录账号"
                clearable
                v-model="form.username"
              />
            </el-form-item>

            <el-form-item label="真实姓名" prop="realName">
              <el-input
                style="width: 200px"
                placeholder="请输入真实姓名"
                clearable
                v-model="form.realName"
              />
            </el-form-item>

            <el-form-item label="邮箱" prop="email">
              <el-input placeholder="请输入用户邮箱" clearable v-model="form.email"  style="width: 200px" />
            </el-form-item>

            <el-form-item label="手机号码" prop="phone">
              <el-input placeholder="请输入手机号码" clearable v-model="form.phone"  style="width: 200px" />
            </el-form-item>

            <el-form-item label="性别" prop="gender" >
              <el-select v-model="form.gender" placeholder="请选择用户性别" clearable  style="width: 200px">
                <el-option
                  v-for="item in dicts.system_global_gender"
                  :key="item.v"
                  :label="item.k"
                  :value="item.v"
                />
              </el-select>
            </el-form-item>

            <el-form-item label="账号状态" prop="enabled" label-width="70px">
              <el-select
                style="width: 200px"
                v-model="form.enabled"
                placeholder="请选择账号状态"
                clearable
              >
                <el-option
                  v-for="item in dicts.system_global_status"
                  :key="item.v"
                  :label="item.k"
                  :value="item.v"
                />
              </el-select>
            </el-form-item>

            <el-form-item>
              <el-button type="primary" @click="handleSearch" icon="el-icon-search">
                搜索
              </el-button>
              <el-button @click="handleReset" icon="el-icon-refresh">
                重置
              </el-button>
            </el-form-item>
            <el-form-item style="display: flex; margin-bottom: 0">
              <el-button
                v-auths="['*:*:*','system:user:add']"
                type="primary"
                @click="handleAdd"
                icon="el-icon-plus">新增</el-button>
              <el-button
                type="danger"
                icon="el-icon-delete"
                v-auths="['*:*:*','system:user:delete']"
               >删除</el-button>
            </el-form-item>
          </el-form>
        </el-header>
        <el-main class="nopadding">
          <el-table :data="userList" height="auto" border
                    paginationLayout="total, prev, pager, next" hideDo>
            <el-table-column align="center" type="selection"/>
            <el-table-column label="用户姓名" prop="username"  align="center" />
            <el-table-column label="真实姓名" prop="realName"  align="center" />
            <el-table-column label="用户类型" prop="userType"  width="100px"  >
              <template #default="{ row }">
                <div v-if="row.userType == 0 ">普通账号</div>
                <div v-if="row.userType == 1 ">超级管理员</div>
              </template>
            </el-table-column>
            <el-table-column label="手机号码" prop="phone"  width="150" />
            <el-table-column label="用户性别" prop="gender"  align="center" >
              <template #default="{row}">
                <template v-for="item in dicts.system_global_gender">
                  <div v-if="row.gender == item.v">{{item.k}}</div>
                </template>
              </template>
            </el-table-column>
            <el-table-column label="账号状态" prop="enabled"  align="center" >
              <template #default="{row}">
                <template v-for="item in dicts.system_global_status">
                  <el-tag v-if="row.enabled == item.v">{{item.k}}</el-tag>
                </template>
              </template>
            </el-table-column>

            <el-table-column label="操作" align="center" width="180" fixed="right">
              <template #default="{ row }">
                <div class="sys-table-main-actions">
                  <el-link
                    icon="el-icon-edit"
                    v-if="!row.userType == 1"
                    type="primary"
                    :underline="false"
                    @click="userUpdate(row)"
                    v-auths="['*:*:*','system:user:update']"
                  >
                    修改
                  </el-link>
                  <el-link
                    icon="el-icon-delete"
                    v-auths="['*:*:*','system:user:delete']"
                    v-if="!row.userType == 1"
                    style="margin-left: 10px"
                    type="danger"
                    :underline="false"
                    @click="userDel(row.id)"
                  >
                    删除
                  </el-link>
                  <el-dropdown>
                    <el-link
                      icon="el-icon-dArrowRight"
                      type="primary" :underline="false" style="margin-top: 6px;margin-left: 10px">
                      更多
                    </el-link>
                    <template #dropdown>
                      <el-dropdown-menu>
                        <el-dropdown-item >
                          <el-link
                            :underline="false"
                            style="margin: 0 4px"
                            class="item"
                            @click="resetPw(row)"
                            v-auths="['*:*:*','system:user:delete']"
                          >
                            重置密码
                          </el-link>
                        </el-dropdown-item>
                        <el-dropdown-item  v-if="!row.userType == 1" >
                          <router-link
                            v-auths="['*:*:*','system:user:assignRole']"
                            :underline="false"
                            style="margin: 0 4px"
                            class="item"
                            :to ="{
                              path:'/system/user/user-assignRole',
                              query:{ id:row.id }
                            }"
                          >
                            分配角色
                          </router-link>
                        </el-dropdown-item>
                      </el-dropdown-menu>
                    </template>
                  </el-dropdown>
                </div>
              </template>
            </el-table-column>
          </el-table>
        </el-main>
        <el-footer style="display: flex;justify-content: flex-end">
          <el-pagination
            :current-page="form.current"
            :page-size="form.size"
            :page-sizes="[10, 20, 50, 100]"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </el-footer>
      </el-container>
    </el-container>
    <user-dialog ref="userDialog" @getUserList="getUserList" ></user-dialog>
  </div>
</template>

<script>
import UserDialog from "./components/user-dialog.vue";

export default {
  name: "index",
  components: { UserDialog  },
  dicts: ['system_global_status','system_global_gender'],
  data(){
    return{
      input:'',
      treeList:[],
      treeProps:{
        label: "name", children: "children"
      },
      ids:[],//选中数组
      form:{
        size:10,
        current:1,
        username:'',
        realName:'',
        email:'',
        phone:'',
        gender:'',
        enabled:'',
        unitId:''
      },
      total:20,
      userList:[],
    }
  },
  created() {
    this.getTreeList()
    this.getUserList()
  },
  watch: {
    input(n) {
      clearTimeout(this.inputTimeout)
      this.inputTimeout = setTimeout(() => {
        this.$refs.treeRef.filter(n)
      }, 500)
    }
  },
  methods:{
    // 获取机构树
    async getTreeList() {
      let res = await this.$API.system.unit.tree.get()
      this.treeList = res.data
    },
    onTreeFilter(value, data) {
      return data.name.includes(value)
    },
    onTreeClick(node){
      this.form.unitId = node.id
      this.getUserList()
    },
    async getUserList(){
      let { data } = await this.$API.system.userManagement.list.get({
        size: this.form.size,
        current: this.form.current,
        username: this.form.username,
        realName: this.form.realName,
        email: this.form.email,
        phone: this.form.phone,
        gender: this.form.gender,
        enabled: this.form.enabled,
        unitId:this.form.unitId
      })
      this.form.size = data.size
      this.total = data.total
      this.form.current = data.current
      this.userList = data.records
    },

    //搜索
    handleSearch(){
      this.getUserList()
    },
    //重置
    handleReset(){
      this.$refs.formRef.resetFields()
      this.form = {}
      this.getUserList()
    },
    //新增
    handleAdd(){
      this.$nextTick(()=>{
        this.$refs.userDialog.open('add').setData()
      })
      this.$refs.userDialog.dialogFormVisible = true
    },
    //修改
    userUpdate(row){
      this.$refs.userDialog.dialogFormVisible = true
      this.$nextTick(()=>{
        this.$refs.userDialog.open('edit').setData(row.id)
      })
    },
    //删除
    async userDel(id){
      this.$confirm('此操作将删除此条数据,是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonClass: '取消',
        type: 'warning'
      }).then(async () => {
        await this.$API.system.userManagement.delete.get(id);
        this.$message({
          type: 'success',
          message: '删除成功'
        })
        this.getUserList()
      })
    },
    //重置密码
    resetPw(row){
      this.$confirm('此操作将重置为初始密码,是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonClass: '取消',
        type: 'warning'
      }).then(async () => {
          await this.$API.system.userManagement.resetPassword.get(row.id);
          this.$message({
            type: 'success',
            message: '重置成功'
          })
          this.getUserList()
      })
    },
    //页码
    handleSizeChange(val) {
      this.searchForm.size = val
      this.getNotice()
    },
    handleCurrentChange(val) {
      this.searchForm.current = val
      this.getNotice()
    },
    // 选中的数组(批量)
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
    }
  }
}
</script>

<style scoped>
.common-layout{
  height: 100%;
}
.tree{
  padding-top: 1rem;
  height: 100%;
}
.sys-filters{
  margin-bottom: 1rem;
}
el-form-item {
  width: 300px;
}
</style>

